import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  userInfo: null, // 存储用户信息
  isLogin: false, // 登录状态
  start: "北京",
  end: "广州",
  date: "2023-01-01",
  isSpeed: true,
  isSeat: true,
  isDesc: true,
};

const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    setUserInfo: (state, action) => {
      state.userInfo = action.payload;
      state.isLogin = true;
    },
    clearUserInfo: (state) => {
      state.userInfo = null;
      state.isLogin = false;
    },

    setCitySelect(state, { payload }) {
      state[payload.type] = payload.val;
    },
    updateCitySelect(state) {
      const cache = state.start;
      state.start = state.end;
      state.end = cache;
    },
  },
});

export const { setUserInfo, clearUserInfo, setCitySelect, updateCitySelect } =
  userSlice.actions;
export default userSlice.reducer;
